/**
 * 
 */
package com.java.test.audit.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.java.test.audit.dto.DepositExamineInfo;
import com.java.test.audit.dto.DrawingExamineInfo;
import com.java.test.audit.dto.RemitExamineInfo;
import com.java.test.audit.service.ExamineManageService;
import com.java.test.controller.BaseController;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.util.HttpErrorCode;
import com.java.test.util.JsonResult;
import com.java.test.util.PageInfo;

/**
 * @author 审核管理
 *
 */
@RestController
@RequestMapping("/examine")
public class ExamineManageController extends BaseController {

	@Autowired
	private ExamineManageService examineManageService;

	/******** ===========================存款审核 ========================= *****/

	/**
	 * 银行卡充值数量统计
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping("/deposbankcardUserCount")
	public JsonResult deposbankcardUserCount(@RequestBody String cards) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			JSONObject json = JSONObject.parseObject(cards);

			cards = json.getString("cards");

			if (cards == null || "".equals(cards)) {
				js.setData(0);
				return js;
			}

			int count = examineManageService.selectbankuserCount(cards);
			js.setData(count);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/depositList")
	public JsonResult depositList(@RequestBody PageInfo<DepositExamineInfo> page) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PageInfo<DepositExamineInfo> info = examineManageService.selectrunwaterInfoList(page);
			if (info == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(info);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/finddepositInfoById")
	public JsonResult finddepositInfoById(@RequestBody DepositExamineInfo info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			DepositExamineInfo infos = examineManageService.selectDepositExamineInfoById(info.getId());
			if (infos == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(infos);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updatedepositInfoByIdPL1")
	public JsonResult updatedepositInfoByIdPL1(@RequestBody List<DepositExamineInfo> info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());

		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			int[] ids = info.stream().mapToInt(DepositExamineInfo::getId).toArray();
			examineManageService.updateDepositExamineInfoById(ids, 1, user.getLoginAccout(), null);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updatedepositInfoByIdPL0")
	public JsonResult updatedepositInfoByIdPL0(@RequestBody List<DepositExamineInfo> info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			int[] ids = info.stream().mapToInt(DepositExamineInfo::getId).toArray();
			examineManageService.updateDepositExamineInfoById(ids, 2, user.getLoginAccout(), info.get(0).getContent());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updatedepositInfoById1")
	public JsonResult updatedepositInfoById1(@RequestBody DepositExamineInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			int[] ids = new int[1];
			ids[0] = info.getId();
			examineManageService.updateDepositExamineInfoById(ids, 1, user.getLoginAccout(), null);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/******** ===========================提款审核 ========================= *****/

	@RequestMapping("/drawingList")
	public JsonResult drawingList(@RequestBody PageInfo<DrawingExamineInfo> page) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PageInfo<DrawingExamineInfo> info = examineManageService.selectdrawingInfoList(page);
			if (info == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(info);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}
	
	
	
	@RequestMapping("/selectdrawingInfoByid")
	public JsonResult selectdrawingInfoByid(@RequestBody DrawingExamineInfo infos) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			DrawingExamineInfo info = examineManageService.selectdrawingInfoByid(infos);
			if (info == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(info);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}
	

	@RequestMapping("/updatedrawingInfoById")
	public JsonResult updatedrawingInfoById(@RequestBody DepositExamineInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			int[] ids = new int[1];
			ids[0] = info.getId();
			examineManageService.updateExamineInfo(ids, info.getStatus(), user.getLoginAccout(),info.getContent());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updatedrawingInfoByIdPL1")
	public JsonResult updatedrawingInfoByIdPL1(@RequestBody List<DepositExamineInfo> info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			int[] ids = info.stream().mapToInt(DepositExamineInfo::getId).toArray();
			examineManageService.updateExamineInfo(ids, 1, user.getLoginAccout(),info.get(0).getContent());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updatedrawingInfoByIdPL0")
	public JsonResult updatedrawingInfoByIdPL0(@RequestBody List<DepositExamineInfo> info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			int[] ids = info.stream().mapToInt(DepositExamineInfo::getId).toArray();
			examineManageService.updateExamineInfo(ids, 2, user.getLoginAccout(),info.get(0).getContent());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/************* ===============待汇款================ ***************/

	@RequestMapping("/remitList")
	public JsonResult remitList(@RequestBody PageInfo<RemitExamineInfo> page) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PageInfo<RemitExamineInfo> info = examineManageService.selectremitInfoList(page);
			if (info == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(info);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updateremitInfoById")
	public JsonResult updateremitInfoById(@RequestBody RemitExamineInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			examineManageService.updateremitExaminInfo(info.getId(), user.getLoginAccout());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

}
